package edu.zut.lxc.loveime;


import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;


import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;




import java.util.List;

import edu.zut.lxc.loveime.constants.Constants;
import edu.zut.lxc.loveime.service.FloatingWindowService;


public class MainActivity extends AppCompatActivity implements View.OnClickListener{


    private static final int SYSTEM_FLOAT_WINDOW = 2;
    public static int width = 360;

   private final int WIFI_WRITE_INSTALL = 1;

   private final static String TAG = "MainActivity";

    private SharedPreferences sharedPreferences;

    private EditText api_key_edit;

    private EditText base_url_edit;
    private EditText Modal_edit;


    private Context context;

    String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent = new Intent(this, FloatingWindowService.class);

        startService(intent);

        Log.e(TAG,"我启动了");

        sharedPreferences =  getSharedPreferences("user_info", Context.MODE_PRIVATE);

//        XUpdate.newBuild(getContext())
//                .apkCacheDir(PathUtils.getExtDownloadsPath())
//                .updateHttpService(XUpdateServiceParser.getUpdateHttpService())
//                .supportBackgroundUpdate(true)
//                .updateUrl(Constants.UPDATE_DEFAULT_URL)
//                .update();



        findViewById(R.id.btn_go_settings).setOnClickListener(this);
        findViewById(R.id.btn_select_keyboard).setOnClickListener(this);

        findViewById(R.id.saveButton).setOnClickListener(this);

        api_key_edit = findViewById(R.id.api_key);
        context = this;
        Modal_edit = findViewById(R.id.Modal);
        base_url_edit = findViewById(R.id.base_url);

        WindowManager manager = this.getWindowManager();
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);

        int width = outMetrics.widthPixels;
       int grid_width = (int) (width*0.8f - width*0.015f*2);


        String api_key = sharedPreferences.getString("api_key", "空");
        String base_url = sharedPreferences.getString("base_url", "http://39.101.179.14:3000");

        if (!api_key.equals("空")){
            api_key_edit.setText(api_key);
        }
        if (!base_url.equals("空")){
            base_url_edit.setText(base_url);
        }

        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.remove("content");
        editor.putInt("pagerWidth", grid_width);
        editor.commit();

    }


    @SuppressLint("ResourceAsColor")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_go_settings:

                Intent intent = new Intent();
                intent.setAction(Settings.ACTION_INPUT_METHOD_SETTINGS);
                startActivity(intent);
                break;

            case R.id.btn_select_keyboard:

//                getStatusBarHeight(getApplicationContext());
                ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).showInputMethodPicker();
                break;

            case R.id.saveButton:
                //保存api_key和base_url

                String api_key = api_key_edit.getText().toString();
                String base_url = base_url_edit.getText().toString();
                String modal = Modal_edit.getText().toString();

                //简单的逻辑判断
                if(TextUtils.isEmpty(api_key_edit.getText().toString())){
                    Toast.makeText(this, "API_KEY不能为空", Toast.LENGTH_SHORT).show();

                } else {

                    if (TextUtils.isEmpty(base_url)){
                        base_url="http://39.101.179.14:3000";
                    }
                    if (TextUtils.isEmpty(modal)){
                        modal = "deepseek-chat";
                    }
                    //保存api_key和base_url
                    SharedPreferences.Editor editor = getSharedPreferences("user_info", MODE_PRIVATE).edit();
                    editor.putString("api_key", api_key);
                    editor.putString("base_url", base_url);
                    editor.putString("modal", modal);
                    editor.commit();
                    Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
                }
        }
    }

    @Override
    protected void onDestroy() {
        Intent intent = new Intent(this,FloatingWindowService.class);
        stopService(intent);
        super.onDestroy();
    }




}
